package com.vcc.service.impl;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.util.DateUtil;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.ClearTran;
import com.vcc.data.entity.Role;
import com.vcc.data.entity.UserCard;
import com.vcc.data.mapper.ClearTranMapper;
import com.vcc.service.service.ClearTranService;
import com.vcc.service.service.UserCardService;
import com.vcc.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.vcc.data.entity.table.ClearTranTableDef.CLEAR_TRAN;
import static com.vcc.data.entity.table.UserTableDef.USER;

@Service
public class ClearTranServiceImpl extends ServiceImpl<ClearTranMapper, ClearTran> implements ClearTranService {

    @Autowired
    private UserService userService;
    @Autowired
    private UserCardService userCardService;
    
    @Override
    public Page<ClearTran> paging(Long userId, Role role, Page<ClearTran> pageable, Long targetUserId, String type, String cardNumber, String startDate, String endDate) {
        QueryChain<ClearTran> queryChain = queryChain();
        if (role.getHasAllData()){
            if (RegexUtil.checkObjectIsNotNull(targetUserId)) {
                queryChain.eq(ClearTran::getUserId, targetUserId);
            } else {
                queryChain.leftJoin(USER).on(CLEAR_TRAN.USER_ID.eq(USER.ID)).where(
                        USER.PARENT_ID.eq(userId).or(USER.ID.eq(userId))
                );
            }
        }else {
            queryChain.eq(ClearTran::getUserId, userId);
        }
        if (RegexUtil.checkStringIsNotNull(type)) {
            queryChain.eq(ClearTran::getType, type);
        }
        if (RegexUtil.checkObjectIsNotNull(startDate)) {
            queryChain.ge(ClearTran::getTranDate, DateUtil.getDayBeginDate(startDate,"yyyy-MM-dd"));
        }
        if (RegexUtil.checkObjectIsNotNull(endDate)) {
            queryChain.le(ClearTran::getTranDate, DateUtil.getDayEndDate(endDate,"yyyy-MM-dd"));
        }
        return queryChain.page(pageable);
    }

    @Override
    public Page<ClearTran> paging(Long userId, Page<ClearTran> pageable, Long targetUserId, String type, String cardNumber, String startDate, String endDate) {
        QueryChain<ClearTran> queryChain = queryChain();

        if (RegexUtil.checkStringIsNotNull(cardNumber)) {
            UserCard userCard = userCardService.getByCardNumber(cardNumber);
            queryChain.eq(ClearTran::getCardNumber, (userCard.getCardNumber()));
        }
        if (RegexUtil.checkObjectIsNotNull(targetUserId)) {
            queryChain.eq(ClearTran::getUserId, targetUserId);
        }

        if (RegexUtil.checkStringIsNotNull(type)) {
            queryChain.eq(ClearTran::getType, type);
        }
        if (RegexUtil.checkObjectIsNotNull(startDate)) {
            queryChain.ge(ClearTran::getTranDate, DateUtil.getDayBeginDate(startDate, "yyyy-MM-dd"));
        }
        if (RegexUtil.checkObjectIsNotNull(endDate)) {
            queryChain.le(ClearTran::getTranDate, DateUtil.getDayEndDate(endDate, "yyyy-MM-dd"));
        }
        return queryChain.page(pageable);
    }

    @Override
    public Boolean existsByClearTransactionId(String clearTransactionId) {
        return queryChain().eq(ClearTran::getTransactionId, clearTransactionId).exists();
    }
}
